package server;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import viewModel.PasswordBack;
import viewModel.Root;
import viewModel.SecretQuestion;
import viewModel.UserSecurityInfo;
import code.Code;
import dao.UserTab;
import dao.UserTabDAO;

@Service
public class UserServer {

	@Autowired
	private UserTabDAO userTabDAO;
	public void register() {
		UserTab userTab=new UserTab();
		userTab.setUserAcc("account1");
		userTab.setPwdNor("password1");
		userTabDAO.save(userTab);
	}
	public String check(String acc, String pwd,HttpSession session) {
		java.util.List<UserTab> usList=userTabDAO.findByUserAcc(acc);
		if(usList.size()==0){
			return "error";
		}
		if(usList.get(0).getAccountStatus()!=Code.active){
			return "freeze";
		}
		if( usList.get(0).getPwdNor().equals(pwd)){
			session.setAttribute("nickName", usList.get(0).getNickName()==null?usList.get(0).getUserAcc():usList.get(0).getNickName());
			return "success";
		}else{
			return "error";
		}
	}
	public String regcheckname(String name) {
		java.util.List<UserTab> usList=userTabDAO.findByUserAcc(name);
		if(usList.size()>0){
			return Code.Error;
		}
		return Code.Success;
	}
	public String checkIdCard(String idcard) {
		if("".equals(idcard)||null==idcard){
			return Code.ErrorIDCard;
		}
		java.util.List<UserTab> usList=userTabDAO.findByIdNumber(idcard);
		if(usList.size()>0){
			return Code.RepeatIDCard;
		}
		return Code.Success;
	}
	public void acc(UserSecurityInfo securityInfo) {
		UserTab userTab;
		boolean flag=false;
		if(userTabDAO.findByUserAcc(securityInfo.getUseraccount()).size()>0){
			flag=true;
			userTab=userTabDAO.findByUserAcc(securityInfo.getUseraccount()).get(0);
		}else{
			userTab=new UserTab();
		}
		userTab.setUserAcc(securityInfo.getUseraccount());
		userTab.setIdNumber(securityInfo.getPersonid());
		userTab.setPwdNor(securityInfo.getPassword());
		userTab.setTrueName(securityInfo.getRealname());
		userTab.setCreateTime(new Timestamp(new Date().getTime()));
		userTab.setSecurityLv(securityInfo.getSecurityLv());
		userTab.setAccountStatus(securityInfo.getAccountStatus());
		userTab.setEmailUrl(securityInfo.getEmailUrl());
		userTab.setNickName(securityInfo.getUseraccount());
		userTab.setPhone(securityInfo.getPhone());
		userTab.setAvatar("defaultAvatar.gif");
		if(flag){
			userTabDAO.attachDirty(userTab);
		}else{
			userTabDAO.save(userTab);
		}
	}
	public String checkEmail(String email) {
		if("".equals(email)||null==email){
			return Code.Error;
		}
		java.util.List<UserTab> usList=userTabDAO.findByUserAcc(email);
		if(usList.size()>0){
			return Code.Error;
		}
		return Code.Success;
	}
	public void secretEmailForm(String account, String emailUrl) {
		UserTab userTab=userTabDAO.findByUserAcc(account).get(0);
		userTab.setSecretEmailUrl(emailUrl);
		userTab.setSecretEmailStatus(String.valueOf(Code.freeze));
		userTabDAO.attachDirty(userTab);
	}
	public String secretemailurl(String email, String code) {
		UserTab userTab=userTabDAO.findBySecretEmailUrl(code).get(0);
		if(userTab.getSecretEmailStatus().equals(String.valueOf(Code.active))){
			return "activeSuccess";
		}
		userTab.setMail(email);
		userTab.setSecretEmailStatus(String.valueOf(Code.active));
		userTabDAO.attachDirty(userTab);
		return "activeSuccess";
	}
	public void secretQuestionForm(SecretQuestion question, String attribute) {
		UserTab userTab=userTabDAO.findByUserAcc(attribute).get(0);
		userTab.setQuestion1(question.getQuestion1());
		userTab.setQuestion2(question.getQuestion2());
		userTab.setQuestion3(question.getQuestion3());
		userTab.setAnswer1(question.getAnswer1());
		userTab.setAnswer2(question.getAnswer2());
		userTab.setAnswer3(question.getAnswer3());
		userTabDAO.attachDirty(userTab);
	}
	public void modifyNickname(HttpSession session, String nickname) {
		String accName=session.getAttribute("name").toString();
		UserTab userTab=userTabDAO.findByUserAcc(accName).get(0);
		userTab.setNickName(nickname);
		userTabDAO.attachDirty(userTab);
	}
	public void setNewPwd(String accName, String newPwd) {
		UserTab userTab=userTabDAO.findByUserAcc(accName).get(0);
		userTab.setPwdNor(newPwd);
		userTabDAO.attachDirty(userTab);
	}
	public ArrayList<UserTab> checkuser(String useraccount) {
		return (ArrayList<UserTab>) userTabDAO.findByUserAcc(useraccount);
	}
	public String checkAnswer(PasswordBack back) {
		UserTab userTab=userTabDAO.findByUserAcc(back.getUserAccount()).get(0);
		if(!back.getAnswer1().equals(userTab.getAnswer1())){
			return "error";
		}
		if(!back.getAnswer2().equals(userTab.getAnswer2())){
			return "error";
		}
		if(!back.getAnswer3().equals(userTab.getAnswer3())){
			return "error";
		}
		return "success";
	}
	public void passwordBackByEmail(String userAccount, String uuid) {
		UserTab userTab=userTabDAO.findByUserAcc(userAccount).get(0);
		userTab.setPwdBackCode(uuid);
		userTabDAO.attachDirty(userTab);
	}
	public String checkEmailCode(String userAccount, String emailCode) {
		UserTab userTab=userTabDAO.findByUserAcc(userAccount).get(0);
		if(emailCode.equals(userTab.getPwdBackCode())){
			return "success";
		}
		return "error";
	}

}
